﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LoginModule;
namespace HDVN5.Account
{
    public partial class DangKyThanhLap : System.Web.UI.Page
    {
        private DanhMucDataContext db = new DanhMucDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                mauKhan.DataSource = db.QDBANDIEUHANH_MAUVIENKHANs.ToArray();
                mauKhan.DataBind();

                TinhThanhPho.DataSource = db.DANHMUCTINHTHANHs.ToArray();
                TinhThanhPho.DataBind();
            }
        }

        protected void btnDangKy_Click(object sender, EventArgs e)
        {
            themMoiDonVi();
                
        }


        private void themMoiDonVi()
        {

            DateTime ngayThanhLap = DateTime.Parse(Request.Form["ngaythanhlap"]);
            DateTime ngayDangKy = DateTime.Parse(Request.Form["ngaydangky"]);
            string tenDonVi = Request.Form["tendonvi"];
            string truong = Request.Form["truong"];
            string email = Request.Form["Email"];
            int tinh = Int16.Parse(TinhThanhPho.SelectedValue.ToString());
            int cap = Int16.Parse(Request.Form["capDonVi"]);
            int option = Int16.Parse(Request.Form["optionKhan"]);
            int mau = 0;
            long id = Convert.ToInt32(LoginModule.CongCu.CapMaSo.capMaSoDangKyDonVi(tinh, cap));

            if (option == 2) mau = Int16.Parse(mauKhan.SelectedValue.ToString());

            LoginModule.SOQUANLYDONVI themMoiDonVi = new LoginModule.SOQUANLYDONVI();
            themMoiDonVi.MASO = Convert.ToInt32(id);
            themMoiDonVi.TENDONVIDANGKY = tenDonVi;
            themMoiDonVi.TINHTHANH = tinh;
            themMoiDonVi.NGAYDANGKY = ngayDangKy;
            themMoiDonVi.NGAYTAO = ngayThanhLap;
            themMoiDonVi.CAPID = cap;
            themMoiDonVi.MAUVIENDONVI = mau;
            themMoiDonVi.EMAIL = email;
            themMoiDonVi.TENTRUONG = truong;
            themMoiDonVi.HOATDONG = true;
            LoginModule.DangKy dangKy = new LoginModule.DangKy();

            int themMOi = dangKy.dangKyThanhLapDonVi(themMoiDonVi);

            if (themMOi == 1 && cap == 1)
            {
                string liendoan = Request.Form["danhsachliendoan"];
                liendoan = liendoan.Substring(0, liendoan.LastIndexOf(";"));
                string[] arrData = liendoan.Split(';');

                for (int i = 0; i < arrData.Length; i++)
                {
                    string[] arr = arrData[i].Split(',');

                    LoginModule.SOQUANLYDONVI themLienDoan = new LoginModule.SOQUANLYDONVI();
                        themLienDoan.MASO = Convert.ToInt32(LoginModule.CongCu.CapMaSo.capMaSoDangKyDonVi(tinh, 4));
                        themLienDoan.TENDONVIDANGKY = arr[0].Substring(1);
                        themLienDoan.TINHTHANH = tinh;
                        themLienDoan.NGAYDANGKY = ngayDangKy;
                        themLienDoan.NGAYTAO = ngayThanhLap;
                        themLienDoan.CAPID = 4;
                        themLienDoan.MAUVIENDONVI = mau;
                        themLienDoan.EMAIL = arr[2].Substring(0,arr[2].LastIndexOf('}'));
                        themLienDoan.TENTRUONG = arr[1];
                        themLienDoan.MACAPTRUCTHUOC = Convert.ToInt32(id);
                        themLienDoan.HOATDONG = true;
                        LoginModule.DangKy dangKyLD = new LoginModule.DangKy();

                        int themMOi1 = dangKyLD.dangKyThanhLapDonVi(themLienDoan);
             
                }

                Response.Redirect("/Account/DangKyThanhLapThanhCong.aspx?id=" + id);
            }

          

        }

    }
}